package com.example.bsxk.web;

import com.example.bsxk.dto.ZyxBaseResponse;
import com.example.bsxk.dto.ZyxStudentChoosedThesis;
import com.example.bsxk.dto.ZyxStudentThesis;
import com.example.bsxk.service.KZTeacherChoiceStudentService;
import com.example.bsxk.service.ZyxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

//@Controller
@RestController
public class ZyxStudentChooseThesis {
    @Autowired
    ZyxUserService service;
    @Autowired
    KZTeacherChoiceStudentService studentService;

    //已选和未选课程
    @GetMapping("/student/{id}/theses")
    public List<List<ZyxStudentChoosedThesis>> findChooseThesis(@PathVariable("id") Long id, Boolean choose,@RequestParam(value = "compact",required = false,defaultValue = "false") Boolean compact){
        List<List<ZyxStudentChoosedThesis>> list = new ArrayList<>();
        if (choose){
            //已选课题
            List<ZyxStudentChoosedThesis> chooseThesesById = service.findChooseThesesById(id);
            list.add(chooseThesesById);
        }
        choose = false;
        if (choose && compact){
            List<ZyxStudentChoosedThesis> chooseThesesById2 = service.findOptionalThesesById(id);
            list.add(chooseThesesById2);
        }else {
            List<ZyxStudentChoosedThesis> chooseThesesById3 = service.findNotChooseThesesById(id);
            list.add(chooseThesesById3);
        }
        return list;
    }


//选课
    @PostMapping("/student/{id}/theses/{tid}")
//    @ResponseBody
    public ZyxBaseResponse chooseThesis(@PathVariable("id") Long id, @PathVariable("tid") Integer tid, ZyxStudentThesis thesis){
        ZyxBaseResponse response = new ZyxBaseResponse();

        int thesisCount = service.findThesisCount(tid);
        int userCount = service.findThesisCountById(id);

        if (thesisCount >= 3){
            response.setCode("2000");
            response.setMessage("该课程已被选满！");
            return response;
        }
        if (userCount >= 3 ){
            response.setCode("2002");
            response.setMessage("你选择的课题不能超过3个！");
            return response;
        }
        if (studentService.istrue(tid,id)){
            return ZyxBaseResponse.fail2001();
        }

        service.insertThesis(thesis);
        return ZyxBaseResponse.success();
    }


    //删除选课
    @DeleteMapping("/student/{id}/theses/{tid}")
    public ZyxBaseResponse deleteByid(@PathVariable("id") Long id, @PathVariable("tid") Integer tid){
        ZyxStudentThesis thesis = new ZyxStudentThesis();
        thesis.setUid(id);
        thesis.setTid(tid);
        if (studentService.istrue(tid,id)){
            return ZyxBaseResponse.fail2001();
        }
        service.deleteById(thesis);
        return ZyxBaseResponse.success();
    }


}
